<template>
  <div class="app-container">
    <div class="header">
      <el-form :inline="true" class="demo-form-inline" size="small">
        <el-form-item label="stuId">
          <el-input v-model="query.nick" placeholder="请输入学号" />
        </el-form-item>
        <select style="height: 30px;margin: 0 20px;border: 1px solid #e7d9d2;color:#847973 ">
          <option>成绩管理</option>
          <option>个人信息</option>
          <option>评分信息</option>
        </select>
        <el-form-item>
          <el-button type="primary" @click="getPage">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="multipleTable"
      v-loading="loading"
      max-height="300px"
      tooltip-effect="dark"
      style="width: 100%"
      :data="tableData"
      border
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        prop="stuId"
        label="学号"
        align="center"
      />
      <el-table-column
        prop="stuName"
        label="姓名"
        align="center"
      />
      <!--      <el-table-column
        prop="age"s
        label="消费日期" align="center"
      >
      </el-table-column>-->
      <el-table-column
        prop="score"
        label="成绩"
        width="180"
      />
      <el-table-column
        prop="tecName"
        s
        label="授课老师"
        align="center"
      />
      <!--      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <el-pagination
      class="pagination-container"
      background
      :current-page="query.current"
      :page-sizes="[1,10, 20, 30, 40]"
      :page-size="query.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="query.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <el-dialog
      title="添加用户数据"
      :visible.sync="dialogVisible"
    >

      <el-form ref="form" inline label-width="120" size="small">
        <el-form-item prop="userName" label="用户名">
          <el-input v-model="user.userName" type="text" clearable />
        </el-form-item>
        <el-form-item prop="nickName" label="姓名">
          <el-input v-model="user.nickName" type="text" clearable />
        </el-form-item>
        <el-form-item prop="age" label="年龄">
          <el-input-number v-model="user.age" clearable />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { add, del, edit, get, list, page } from '@/api/table'

export default {
  data() {
    return {
      tableData: [],
      query: {
        current: 1,
        total: 0,
        size: 10,
        nick: ''
      },
      user: {
        id: undefined,
        userName: '',
        age: 0,
        nickName: ''
      },
      student: {
        stuId: undefined,
        stuName: '',
        stuSex: '',
        stuAge: 0
      },
      teacher: {
        tecId: undefined,
        tecName: ''
      },
      score: {
        couId: undefined,
        couName: '',
        score: ''
      },
      course: {
        couId: undefined,
        couName: '',
        tecId: undefined
      },
      /* methods: {
        handleClick() {
          alert('button click');
        }
      },*/
      loading: false,
      dialogVisible: false,
      multipleSelection: []
    }
  },
  created() {
    // this.getList()
    this.getPage()
  },
  methods: {
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    getList() {
      list().then((resp) => {
        this.tableData = resp.data
      }).catch((err) => {
        console.log(err)
      })
    },
    getPage() {
      this.loading = true
      page(this.query).then((resp) => {
        this.tableData = resp.data
        this.query.total = resp.total
        this.loading = false
      })
    },
    handleSizeChange(val) {
      this.query.size = val
      this.getPage()
    },
    handleCurrentChange(val) {
      this.query.current = val
      this.getPage()
    },
    handAdd() {
      this.dialogVisible = true
    },
    submit() {
      const messageBox = this.$message({
        message: '正在提交数据，请稍后',
        iconClass: 'el-icon-loading'
      })
      // eslint-disable-next-line eqeqeq
      if (this.user.id != undefined) {
        edit(this.user).then(resp => {
          this.user = {}
          this.getPage()
          messageBox.close()
          this.dialogVisible = false
          this.$message.success('修改成功')
        })
      } else {
        add(this.user).then((resp) => {
          this.user = {}
          this.getPage()
          messageBox.close()
          this.dialogVisible = false
          this.$message.success('添加成功')
        })
      }
    },
    handleEdit(row) {
      get(row.id).then(resp => {
        this.user = resp.data
        this.dialogVisible = true
      })
    },
    handleDelete(row) {
      this.$confirm('是否继续删除数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.id).then(resp => {
          this.getPage()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {

      })
    }
  }
}
</script>
<style>

</style>
